package com.lgst.ticket;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class SearchActivity extends AppCompatActivity {

    private ArrayList<TicketInfo> mTickets;
    private Toolbar mToolbar;
    private RecyclerView mClv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        mTickets = getIntent().getParcelableArrayListExtra("values");
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mClv = (RecyclerView) findViewById(R.id.clv);
        mClv.setLayoutManager(new LinearLayoutManager(this));
        mClv.setAdapter(new ClvAdapter());
    }

    class ClvAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.item_search_ticket, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            TicketInfo ti = mTickets.get(position);
            vh.mDestination.setText(ti.getDestination());
            vh.mDate.setText(ti.getDate());
            vh.mTicketNum.setText(ti.getTicketNum());
            vh.mPrice.setText(ti.getPrice());
            vh.mCarType.setText(ti.getCarType());
            vh.mTime.setText(ti.getTime());
            vh.mStation.setText(ti.getStation());
        }

        @Override
        public int getItemCount() {
            return mTickets.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            View rootView;
            TextView mDestination;
            TextView mDate;
            TextView mTime;
            TextView mStation;
            TextView mTicketNum;
            TextView mCarType;
            TextView mPrice;

            ViewHolder(View rootView) {
                super(rootView);
                this.rootView = rootView;
                this.mDestination = (TextView) rootView.findViewById(R.id.destination);
                this.mDate = (TextView) rootView.findViewById(R.id.date);
                this.mTime = (TextView) rootView.findViewById(R.id.time);
                this.mStation = (TextView) rootView.findViewById(R.id.station);
                this.mTicketNum = (TextView) rootView.findViewById(R.id.ticket_num);
                this.mCarType = (TextView) rootView.findViewById(R.id.car_type);
                this.mPrice = (TextView) rootView.findViewById(R.id.price);
            }

        }
    }
}
